layui.use(['form', 'jquery', 'layer', 'table', 'laydate'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var staff_id = '';
	if (staff_form_value.staff_id != undefined) {
		staff_id = staff_form_value.staff_id;
		staff_form_value.master_staff_goods = staff_form_value.master_staff_goods == 1 ? true : false;
		if (staff_form_value.goods_alone == 1) {
			$('.goods-audit-div').hide();
			staff_form_value.goods_alone = true;
			staff_form_value.goods_audit = false;
		} else {
			$('.goods-audit-div').show();
			staff_form_value.goods_alone = false;
			staff_form_value.goods_audit = staff_form_value.goods_audit == 1 ? true : false;
		}
	}
	// 动态获取所属部门
	var get_site_department = function(site_id) {
		var obj = $('select[name="department_id"][lay-filter="department_id' + staff_id + '"]');
		obj.append('<option value="">加载中...</option>');
		obj.prop('disabled', true);
		send_info(get_city_department, {
			site_id: site_id
		}, function(data) {
			var d = data.data;
			obj.html('');
			obj.append('<option value="-1">站点管理员</option>');
			$.each(d, function(n, value) {
				obj.append('<option value="' + value.department_id + '">' + value.department_name + '</option>');
			});
			obj.prop('disabled', false);
			form.render('select');
		})
	}
	if (staff_form_value.site_id == null || staff_form_value.site_id == -1) {
		get_site_department(-1);
	} else {
		get_site_department(staff_form_value.site_id);
	}
	// 监听分站选择
	form.on('select(site_id' + staff_id + ')', function(data) {
		get_site_department(data.value);
	})
	var lay_filter = 'tpl_staff' + staff_id;
	form.val(lay_filter, staff_form_value);
	// 表单渲染
	form.render('checkbox');
	form.render('select');

	// 监听复选框
	form.on('checkbox(staff_goods_alone)', function(data) {
		var check_name = $(this).attr('name');
		var value = data.elem.checked;
		if (value == true) {
			$('.goods-audit-div').hide();
		} else {
			$('.goods-audit-div').show();
		}
	})
	// 监听提交
	form.on('submit(submit_staff)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		if (field.site_id == '') {
			layer.msg('请选择分站', {
				icon: 2,
				amim: 6
			})
			$('input[name="site_id"]').addClass('.layui-form-danger').focus();
			return false;
		}
		if (field.staff_name == '') {
			layer.msg('部门名称必须填写', {
				icon: 2,
				amim: 6
			})
			$('input[name="staff_name"]').addClass('.layui-form-danger').focus();
			return false;
		}
		send_info(url, field, function(data) {
			if (data.code == 0) {
				layer.msg('保存成功', {
					icon: 1
				});
				// 关闭当前页 刷新父页
				$('.layui-body .layui-tab[lay-filter="content"] li.layui-this .layui-tab-close').trigger('click');
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="staff"]').trigger('click');
				// 父页面点击
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="staff_list"]').trigger('click');
				// 表格重载
				if ($('table[lay-filter="staff_list"]').length > 0) {
					if (field.staff_id == undefined || field.staff_id == '') {
						table.reload('staff_list');
					} else {
						table.editTr('staff_list', 'staff_id', field.staff_id);
					}
				}
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		})
		return false;
	})

})
